﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ExcelDna.Integration;
using ExcelDna.Integration.Extensibility;
using FastLogAsync;
using ExcelDna.Integration.CustomUI;
using System.Windows.Forms;

namespace ExcelDnaDemo
{
    [Guid(AppSettings.DemoAddinGUID), ProgId(AppSettings.DemoAddinProgID)]
    [ComVisible(true)]
    class ComAddInConnection : ExcelComAddIn
    {
        #region IDTExensibility2
        /// <summary>
        /// Receives notification that the Add-in is being loaded.
        /// </summary>
        public override void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {            
            Log.Info("OnConnection");
            
            switch (ConnectMode)
            {
                //如果希望程序简单的话，我觉得也可以都写在ext_cm_Startup里面.
                //https://docs.microsoft.com/zh-cn/dotnet/api/extensibility.ext_connectmode?view=visualstudiosdk-2017 ext_Connect 模式 枚举
                case ext_ConnectMode.ext_cm_AfterStartup:  //在加载项管理器里面加载插件时触发，不会运行OnStartupComplete
                    Log.Info("OnConnection ext_cm_AfterStartup");
                    OnStartupComplete(ref custom);
                    break;
                case ext_ConnectMode.ext_cm_Startup:       //Excel中打开VBE，第一次加载插件时触发，会运行OnStartupComplete
                    Log.Info("OnConnection ext_cm_Startup");
                    //如果插件有什么只初始化一次的工作（即使反复加载插件），在这里进行。
                    //todo...
                    //会运行OnStartupComplete
                    break;
                default:
                    //正常不会进入这里，因为大部分程序只有两种加载方式。
                    System.Windows.Forms.MessageBox.Show("ConnectMode is neither AfterStartup nor Startup!");
                    break;
            }

            base.OnConnection(Application, ConnectMode, AddInInst, ref custom);
        }
        /// <summary>
        /// Receives notification that the Add-in is being unloaded.
        /// </summary>
        public override void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
            Log.Info("OnDisconnection");
            if (RemoveMode != ext_DisconnectMode.ext_dm_HostShutdown)
            {
                OnBeginShutdown(ref custom);
            }
            base.OnDisconnection(RemoveMode, ref custom);
        }
        /// <summary>
        /// Receives notification when the collection of Add-ins has changed.
        /// </summary>
        public override void OnAddInsUpdate(ref Array custom)
        {
            base.OnAddInsUpdate(ref custom);
        }
        /// <summary>
        /// Receives notification that the host application has completed loading.
        /// </summary>
        public override void OnStartupComplete(ref Array custom)
        {
            Log.Info("OnStartupComplete");
            //如果插件有什么每次启动都要初始化的工作，在这里进行。
            //todo...
            base.OnStartupComplete(ref custom);
        }
        /// <summary>
        /// Receives notification that the host application is being unloaded.
        /// </summary>
        public override void OnBeginShutdown(ref Array custom)
        {
            base.OnBeginShutdown(ref custom);
        }
        #endregion IDTExensibility2

        #region ICustomTaskPaneConsumer成员
        /*
        //在加载xll时，得不到CTPFactory实例
        public void CTPFactoryAvailable(ICTPFactory CTPFactoryInst)
        {
            try
            {
                CTPManager.Init(CTPFactoryInst);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Creat CTP error!\r\n" + exception.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        */
        #endregion ICustomTaskPaneConsumer成员
    }
}
